package com.mingyizhudao.app.view.mine;

import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.utils.AlertDialogs;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.igexin.sdk.PushManager;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.MYBaseFragment;
import com.mingyizhudao.app.view.WebViewActivity;
import com.mingyizhudao.app.view.home.bean.ScalpelEntity;
import com.mingyizhudao.app.view.login.LoginActivity2;
import com.mingyizhudao.app.view.mine.bean.BookingStatusEntity;
import com.mingyizhudao.app.view.mine.bean.OrderItemEntity;
import com.mingyizhudao.app.view.mine.bean.OrderNumEntity;
import com.mingyizhudao.app.view.mine.bean.OrderNumEntity.NumEntity;

public class MineFragment extends MYBaseFragment{

	private TextView user_code;
	private ImageView avtar;
	private boolean  isLogin = false;
	private RelativeLayout view_loginout;
	
	
	private TextView tv_unrend1,tv_unrend2,tv_unrend3,tv_unrend4;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mine, container, false); 
		initView(view);
		return view;
	}

	private void initView(View view){
		view.findViewById(R.id.view_order).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_report).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_problem).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_about).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_call).setOnClickListener(onClickListener);
		view.findViewById(R.id.view_gongyi).setOnClickListener(onClickListener);
		
		view.findViewById(R.id.state_1).setOnClickListener(onClickListener);
		view.findViewById(R.id.state_2).setOnClickListener(onClickListener);
		view.findViewById(R.id.state_3).setOnClickListener(onClickListener);
		view.findViewById(R.id.state_4).setOnClickListener(onClickListener);
		
		
		avtar = ( ImageView )view.findViewById(R.id.avtar);
		avtar.setOnClickListener(onClickListener);
		view_loginout = ( RelativeLayout )view.findViewById(R.id.view_loginout);
		view_loginout.setOnClickListener(onClickListener);

		user_code = ( TextView )view.findViewById(R.id.user_code);
		
		tv_unrend1 = (TextView) view.findViewById(R.id.tv_unrend1);
		tv_unrend2 = (TextView) view.findViewById(R.id.tv_unrend2);
		tv_unrend3 = (TextView) view.findViewById(R.id.tv_unrend3);
		tv_unrend4 = (TextView) view.findViewById(R.id.tv_unrend4);
		
		TextView tv_version = (TextView)view.findViewById(R.id.tv_version);
		tv_version.setText("当前版本:" + CommonUtils.getAppVersionName(activity));
		
	}


	@Override
	public void onResume() {

		String mobile =CommonUtils.getMobile( activity );
		String token =CommonUtils.getToken( activity );
		if( token!=null&&token.length()>0&&mobile!=null&&mobile.length()>0 )
		{
			isLogin = true;
			user_code.setText( mobile );
			view_loginout.setVisibility( View.VISIBLE );

		}else {
			user_code.setText( R.string.fm_personal_user_default_code );
			view_loginout.setVisibility( View.GONE );
		}
		
//		if (isLogin) {
//			doNet();
//		}
		super.onResume();
	}

	@Override
	public void initHeaderView() {
		//		clearTitle();
		//		setTopViewBg(R.color.white);
		//		setTitleViewValue( R.string.fragment_person_title, 0, R.color.actionbar_bg_color );
		setTopViewIsShow(false);
	}


	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.avtar:
				if( isLogin )
				{
					Toast.makeText( activity, "您已登录！", 0 ).show();
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}

				break;
			case R.id.view_order:
				if( isLogin )
				{
					IntentHelper.getInstance(activity).gotoActivity(OrderListActivity.class);
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}

				//IntentHelper.getInstance(activity).gotoActivity(OrderDetailActivity.class);

				break;
			case R.id.view_report:
				//IntentHelper.getInstance(activity).gotoActivity(ReportActivity.class);
				break;

			case R.id.view_problem:
				IntentHelper.getInstance(activity).gotoActivity(CommonProblemActivity.class);
				break;
			case R.id.view_about:
				IntentHelper.getInstance(activity).gotoActivity(AboutActivity.class);
				break;
			case R.id.view_call:
				IntentHelper.getInstance(activity).gotoActivity(CallActivity.class);
				break;
			case R.id.view_loginout:
				if(!isLogin){
					Toast.makeText( activity, "您还没登录！", 0 ).show();
				}else{
					AlertDialogs.getAlertDialog(activity).showAlertDialog("您确定要退出登录?", new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							isLogin = false;
							CommonUtils.setToken( activity, null );
//							setAlias();//去掉别名
							
							unBindGeTuiAlias();
							
							user_code.setText( R.string.fm_personal_user_default_code );
							view_loginout.setVisibility( View.GONE );
						}
					});
				}

				break;
			case R.id.view_gongyi:
				    Bundle bundle = new Bundle();//"http://m.mingyizhudao.com/mobile/event/view/page/mygy/header/0"
				   
					//bundle.putString(Constants.PARM_ACTION_URL, CommonUtils.getBenefitUrl(activity));
				    bundle.putString(Constants.PARM_ACTION_URL, "http://m.mingyizhudao.com/mobile/event/view/page/mygy/header/0");
					bundle.putString(Constants.PARM_PAGE_TITLE, "公益手术");
					IntentHelper.getInstance(activity).gotoActivity(WebViewActivity.class, bundle);
				break;
				
			case R.id.state_1:
				if( isLogin )
				{
					goCommonList("1" ,"待支付");
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	
				
			case R.id.state_2:
				if( isLogin )
				{
					goCommonList("2","安排中");
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	
				
			case R.id.state_3:
				
				if( isLogin )
				{
					goCommonList("5","待确认");
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	
				
			case R.id.state_4:
				if( isLogin )
				{
					goCommonList("6","待评价");//6
				}else {
					IntentHelper.getInstance(activity).gotoActivity(LoginActivity2.class);
				}
				break;	

			default:
				break;
			}
		}
	};


	private void goCommonList(String type,String title) {
		Bundle bundle = new Bundle();
		bundle.putString("type", type);
		bundle.putString("title", title);
		IntentHelper.getInstance(activity).gotoActivity(OrderListCommonActivity.class, bundle);
	}
	
	private void doNet() {
		String url = Constants.baseURL+"orderstatuscount"+CommonUtils.getTokenParam(activity);
		getHttpResult(url, Constants.HTTP_INDEX.First, new TypeToken<QjResult<OrderNumEntity>>() {});
	}
	@Override
	public void onSuccess(QjResult result, String flg) {
		super.onSuccess(result, flg);
		if (flg.equals(Constants.HTTP_INDEX.First)) {
			
			OrderNumEntity orderNumEntity= (OrderNumEntity) result.getResults();
			if (orderNumEntity!=null&&orderNumEntity.getUnread()!=null) {
				List<NumEntity> unread =orderNumEntity.getUnread();
				
				tv_unrend1.setVisibility(View.INVISIBLE);
				tv_unrend2.setVisibility(View.INVISIBLE);
				tv_unrend3.setVisibility(View.INVISIBLE);
				tv_unrend4.setVisibility(View.INVISIBLE);
				for (int i = 0; i < unread.size(); i++) {
					if ("1".equals(unread.get(i).getBkStatus())) {
						if (unread.get(i).getNum()!=null&&Integer.parseInt(unread.get(i).getNum())>0) {
							
							tv_unrend1.setVisibility(View.VISIBLE);
							tv_unrend1.setText(unread.get(i).getNum());
						}else {
							tv_unrend1.setVisibility(View.INVISIBLE);
						}
						
						
					}else if("2".equals(unread.get(i).getBkStatus())){
						
                        if (unread.get(i).getNum()!=null&&Integer.parseInt(unread.get(i).getNum())>0) {
							
							tv_unrend2.setVisibility(View.VISIBLE);
							tv_unrend2.setText(unread.get(i).getNum());
						}else {
							tv_unrend2.setVisibility(View.INVISIBLE);
						}
						
					}else if("5".equals(unread.get(i).getBkStatus())){
						
                         if (unread.get(i).getNum()!=null&&Integer.parseInt(unread.get(i).getNum())>0) {
							
							tv_unrend3.setVisibility(View.VISIBLE);
							tv_unrend3.setText(unread.get(i).getNum());
						}else {
							tv_unrend3.setVisibility(View.INVISIBLE);
						}
						
					}else if("6".equals(unread.get(i).getBkStatus())){
						
                        if (unread.get(i).getNum()!=null&&Integer.parseInt(unread.get(i).getNum())>0) {
							
							tv_unrend4.setVisibility(View.VISIBLE);
							tv_unrend4.setText(unread.get(i).getNum());
						}else {
							tv_unrend4.setVisibility(View.INVISIBLE);
						}
						
					}
					
					
					
				}
				
				
				
				
				
			}
			
		}
			
	}
	@Override
	public void onCompleted(Exception e, String flg) {
		super.onCompleted(e, flg);
		LoadingDialog.getInstance(activity).dismiss();
	}
	
	private void unBindGeTuiAlias() {
		String mobile =CommonUtils.getMobile( activity );
	    String alias = "m_"+mobile;
	    
	    alias = Utils.Md5String(alias);
	    
	    if (TextUtils.isEmpty(alias)) {
	        //Toast.makeText(activity,"别名不能为空！", Toast.LENGTH_SHORT).show();
	        return;
	    }
	    if (!CommonUtils.isValidTagAndAlias(alias)) {
	        //Toast.makeText(activity,"无效的别名！", Toast.LENGTH_SHORT).show();
	        return;
	    }

	    // 取消别名
	    PushManager.getInstance().unBindAlias(activity, alias,true);
	}
	
//	private static final int MSG_SET_ALIAS = 1001;
//	private final Handler mHandler = new Handler() {
//	@Override
//	    public void handleMessage(android.os.Message msg) {
//	        super.handleMessage(msg);
//	        switch (msg.what) {
//	            case MSG_SET_ALIAS:
//	                // 调用 JPush 接口来设置别名。
//	                JPushInterface.setAliasAndTags(activity.getApplicationContext(),
//	                                                (String) msg.obj,
//	                                                 null,
//	                                                 mAliasCallback);
//	            break;
//	        default:
//	        }
//	    }                                       
//	};
//	
//	private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
//	    @Override
//	    public void gotResult(int code, String alias, Set<String> tags) {
//	        switch (code) {
//	        case 0:
//	            // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
//	        	
//	            break;
//	        case 6002:
//	            // 延迟 60 秒来调用 Handler 设置别名
//	            mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
//	            break;
//	        default:
//	        }
//	    }
//	};
//	
//	private void setAlias() {
//	    String alias = "";
//	  
//	    // 调用 Handler 来异步设置别名
//	    mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alias));
//	}
}
